home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 341 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.1 KB

  1. Path: solon.com!not-for-mail
  2. From: pete@borland.com (Pete Becker)
  3. Newsgroups: comp.std.c,comp.lang.c.moderated
  4. Subject: Re: Integral promotion.
  5. Date: 21 Feb 1996 09:36:50 -0600
  6. Organization: Borland International
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4gfe6i$33s@solutions.solon.com>
  10. References: <4fstj7$2l6@solutions.solon.com> <4fu835$9de@solutions.solon.com> <4fvgrm$dv9@solutions.solon.com> <4g28hr$qko@solutions.solon.com> <4g54pv$ajt@solutions.solon.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12. X-Newsreader: WinVN 0.99.5
  13.  
  14. In article <4g54pv$ajt@solutions.solon.com>, msb@sq.com says...
  15. :
  16. :I wrote:
  17. :
  18. :> > Thus even in ANSI C, if this function is called this way:
  19. :> >
  20. :> >     extern short p, q, r, test(short,short);
  21. :> >     r = test(p,q);
  22. :> >
  23. :> > the values from p and q are still converted from short to int and back
  24. :> > to short.  
  25. :
  26. :Thad Smith (ThadSmith@acm.org) responded:
  27. :> I find nothing in the Standard that states that the arguments undergo
  28. :> integral promotions before being assigned to the function parameters.
  29. :
  30. :This is in 6.2.1.1/3.2.1.1, the passage which defines the integral
  31. :promotions.  It states that the promotions apply
  32. :
  33. :#  in an expression wherever an int or unsigned int may be used.
  34. :
  35. :Since the call test(1,2) is permissible, this means that p and q must
  36. :be promoted to int.  As noted in my previous posting, they are then
  37. :converted right back to short within the caller (since a prototype
  38. :was used), and the compiler can easily optimize this out.
  39. :
  40.  
  41. This is a somewhat perverse reading of the standard. It does not require 
  42. pointless promotions followed by conversions back to the original type. In 
  43. particular, there is a footnote to this section that says
  44.  
  45.     The integral promotions are applied only as part of the usual
  46.     arithmetic conversions, to certain argument expressions, to the
  47.     operands of the unary +, -, and ~ operators, and to both operands
  48.     of the shift operators, AS SPECIFIED BY THEIR RESPECTIVE SECTIONS.
  49.     [emphasis added]
  50.  
  51. That is, this section does not tell you what must be done every time you 
  52. encounter something that could be promoted to int. It tells you what the term 
  53. "integral promotions" means when it is used in other sections of the working 
  54. paper. In particular, it tells you what "integral promotions" means in the 
  55. context of the "usual arithmetic conversions", which is where it is most often 
  56. encountered.
  57.  
  58. :Another example of this sort of thing is an assignment expression like
  59. :p=q, where p and q are short.  q is promoted to int, then demoted back
  60. :to short; since the value cannot change, the compiler optimizes out the
  61. :conversions.  If you write p=(short)q, then the same reason applies
  62. :to each of the expressions q and (short)q, and you end up with *two*
  63. :round-trip conversions which should be optimized out.
  64.  
  65. No. The description of the semantics of the assignment operator does not 
  66. mention integral promotions. Compare that with the description of the addition 
  67. operator, which says, in part, "If both operands have arithmetic type, the 
  68. usual arithmetic conversions are performed on them."
  69.